//
//  CustomBillViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/6.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class CustomBillViewController: BillRootViewController {
    
    fileprivate var addCustomBillView : AddCustomBillView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = addCustomBill
        addSubView()
    }
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        addCustomBillView = AddCustomBillView.init(frame: self.view.bounds)
        addCustomBillView.delegate = self
        self.view.addSubview(addCustomBillView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

extension CustomBillViewController : AddCustomBillViewDelegate {
    
    //MARK: 选择操作
    func selectOperate(index: Int) {
        
        switch index {
        case 1:
            showDatePickView()
        case 2:
            selectTime()
        case 3:
            saveBillAction()
        default:
            break
        }
    }
    
    //MARK: 选择时间
    fileprivate func selectTime() {
        
        let selectTimeVC = SelectTimeViewController()
        selectTimeVC.modalPresentationStyle = .overCurrentContext
        selectTimeVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        selectTimeVC.timeBlock = {(time) in
            self.addCustomBillView.cycleView.text = time
        }
        self.present(selectTimeVC, animated: false, completion: nil)
    }
    
    //MARK: 显示日期选择框
    fileprivate func showDatePickView() {
        
        let selectDateVC = SelectDateViewController()
        selectDateVC.modalPresentationStyle = .overCurrentContext
        selectDateVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        selectDateVC.dateBlock = {(date) in
            self.addCustomBillView.repaymentView.text = date
        }
        self.present(selectDateVC, animated: false, completion: nil)
    }
    
    //MARK: 保存账单
    fileprivate func saveBillAction() {
        
        if self.addCustomBillView.typeView.textView.text == "" {
            KMTool.showText(_text: inputType)
            return
        }
        
        if self.addCustomBillView.nameView.textView.text == "" {
            KMTool.showText(_text: inputName)
            return
        }
        
        if self.addCustomBillView.amountView.textView.text == "" {
            KMTool.showText(_text: inputAmount)
            return
        }
        
        if ((self.addCustomBillView.typeView.textView.text as NSString?)?.length)! > 5 {
            
            KMTool.showText(_text: typeNotGreate)
            return
        }
        
        if ((self.addCustomBillView.nameView.textView.text as NSString?)?.length)! > 5 {
            
            KMTool.showText(_text: nameNotGreate)
            return
        }
        
        if Int(self.addCustomBillView.amountView.textView.text!)! > 10000 {
            
            KMTool.showText(_text: limitNotGreate)
            return
        }
        
        let userInfo = ["billtype":self.addCustomBillView.typeView.textView.text!,"name":self.addCustomBillView.nameView.textView.text!,"amount":self.addCustomBillView.amountView.textView.text!,"date":self.addCustomBillView.repaymentView.text!,"type":"life"]
        
        NotificationCenter.default.post(name: .addBillPush, object: nil, userInfo: userInfo)
        self.navigationController?.popToRootViewController(animated: true)
    }
}

